package com.mall.db.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.SmsFlashPromotionProductRelationMapper;
import com.mall.pojo.SmsFlashPromotionProductRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsFlashPromotionProductRelationApiImpl implements SmsFlashPromotionProductRelationApi {

    @Autowired
    private SmsFlashPromotionProductRelationMapper relationMapper;

    /**
     * 根据场次id和活动id查询商品数量
     *
     * @param sessionId
     * @param flashPromotionId
     * @return
     */
    @Override
    public Integer findMount(Long sessionId, Integer flashPromotionId) {
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SmsFlashPromotionProductRelation::getFlashPromotionSessionId, sessionId)
                .eq(SmsFlashPromotionProductRelation::getFlashPromotionId, flashPromotionId);
        return relationMapper.selectCount(wrapper);
    }

    /**
     * 根据活动id和场次id查询商品id列表
     *
     * @param pageNum
     * @param pageSize
     * @param flashPromotionId
     * @param flashPromotionSessionId
     * @return
     */
    @Override
    public Page<SmsFlashPromotionProductRelation> findProductIds(Integer pageNum, Integer pageSize, Integer flashPromotionId, Integer flashPromotionSessionId) {
        Page<SmsFlashPromotionProductRelation> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SmsFlashPromotionProductRelation::getFlashPromotionId, flashPromotionId)
                .eq(SmsFlashPromotionProductRelation::getFlashPromotionSessionId, flashPromotionSessionId)
                .orderByDesc(SmsFlashPromotionProductRelation::getSort);
        relationMapper.selectPage(page, wrapper);
        return page;
    }

    /**
     * 修改秒杀关联信息
     * @param flashPromotionProductRelation
     */
    @Override
    public Integer update(SmsFlashPromotionProductRelation flashPromotionProductRelation) {
        Integer i = relationMapper.updateById(flashPromotionProductRelation);
        return i;
    }

    /**
     * 通过三种id查询本场次秒杀活动与该商品的关联是否已存在
     * @param flashPromotionId
     * @param flashPromotionSessionId
     * @param productId
     * @return
     */
    @Override
    public SmsFlashPromotionProductRelation findRelation(Long flashPromotionId, Long flashPromotionSessionId, Long productId) {
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SmsFlashPromotionProductRelation::getFlashPromotionId,flashPromotionId)
                .eq(SmsFlashPromotionProductRelation::getFlashPromotionSessionId,flashPromotionSessionId)
                .eq(SmsFlashPromotionProductRelation::getProductId,productId);
        return relationMapper.selectOne(wrapper);
    }

    /**
     * 保存场次秒杀活动与该商品的关联关系
     * @param relation
     */
    @Override
    public void create(SmsFlashPromotionProductRelation relation) {
        relationMapper.insert(relation);
    }

    /**
     * 删除关联
     * @param id
     * @return
     */
    @Override
    public Integer delete(Integer id) {
        return relationMapper.deleteById(id);
    }
}
